#include"heap.h"

void Swap(int& a, int& b)
{
	int tmp = a;
	a = b;
	b = tmp;
}
void AdjustUp(int* a, int child)
{
	int parent = (child - 1) / 2;
	while (child > 0)
	{
		if (a[child] < a[parent])
		{
			Swap(a[child], a[parent]);
			child = parent;
			parent = (child - 1) / 2;

		}
		else
		{
			break;
		}
	}
}
void heap::heappush(int* a, int x)
{
	if (size == capacity)
	{
		Heapdata* tmp = (Heapdata *)realloc(a,sizeof(Heapdata) * 2 * capacity);
		if (tmp == nullptr)
		{
			perror("realloc fail");
			exit(-1);
		}
		a = tmp;
		capacity *= 2;
	}
	a[size] = x;
	size++;
	AdjustUp(a, size - 1);
}
void heap::heappop(int* a)
{
	Swap(a[0], a[size - 1]);
	size--;
	AdjustUp(a, size - 1);
}
int  heap::heaptop(int* a)
{
	return a[0];
}
int  heap::heapsize(int* a)
{
	return size;
}